package com.itheima.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.Paging;

import java.util.List;

/**
 * <h1>分页查询转换工具类</h1>
 *
 * @author <a href="515857087@qq.com">叶世铭</a>
 * @since 2022/11/25
 */
public final class PageUtils {
    private PageUtils() {
    }

    public static <T> Paging<T> toPage(IPage<T> sourcePageModel) {
        Paging<T> res = new Paging();
        res.setPage(sourcePageModel.getCurrent());
        res.setPerPage(sourcePageModel.getSize());
        res.setList(sourcePageModel.getRecords());
        res.setCount(sourcePageModel.getTotal());
        res.setPageCount(sourcePageModel.getPages());
        return res;
    }

    public static <T, E> Paging<E> toPage(IPage<T> sourcePageModel, List<E> list) {
        Paging<E> res = new Paging();
        res.setPage(sourcePageModel.getCurrent());
        res.setPerPage(sourcePageModel.getSize());
        res.setList(list);
        res.setCount(sourcePageModel.getTotal());
        res.setPageCount(sourcePageModel.getPages());
        return res;
    }
}